package com.example.springstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springstudy.dao.dbo.MenuInfo;
import com.example.springstudy.entity.Menu;
import com.example.springstudy.entity.SkuCategory;
import com.example.springstudy.mapper.SkuCategoryMapper;
import com.example.springstudy.service.SkuCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SkuCategoryServiceImpl extends ServiceImpl<SkuCategoryMapper, SkuCategory> implements SkuCategoryService {
    @Override
    public List<SkuCategory> listAll() {
        List<SkuCategory> list =this.list(new LambdaQueryWrapper<SkuCategory>().eq(SkuCategory::getIsDeleted, 0));
        return parseTree(list);
    }

    @Transactional
    @Override
    public boolean addCategory(SkuCategory SkuCategory) {
        return this.save(SkuCategory);
    }

    @Override
    public boolean updateCategory(SkuCategory skuCategory) {
        return this.updateById(skuCategory);
    }

    @Override
    public boolean deleteCategory(int id) {
        SkuCategory skuCategory = new SkuCategory();
        skuCategory.setId(id);
        skuCategory.setIsDeleted(1);
        return this.updateById(skuCategory);
    }

    private List<SkuCategory> parseTree(List<SkuCategory> list) {
        Set<Integer> ids = list.stream()
                .map(SkuCategory::getId)
                .collect(Collectors.toSet());

        // 获取所有父级ID
        Set<Integer> parentIds = list.stream()
                .map(SkuCategory::getParentId)
                .collect(Collectors.toSet());

        List<Integer> rootIds = parentIds.stream()
                .filter(id -> !ids.contains(id))
                .toList();
        return rootIds.stream()
                .flatMap(rootId->buildTree(rootId, list).stream())
                .collect(Collectors.toList());
    }

    private List<SkuCategory> buildTree(Integer parentId, List<SkuCategory> list) {
        return list.stream()
                .filter(menu-> Objects.equals(menu.getParentId(), parentId))
                .map(entity->{
                    SkuCategory skuCategory = new SkuCategory();
                    skuCategory.setId(entity.getId());
                    skuCategory.setCode(entity.getCode());
                    skuCategory.setName(entity.getName());
                    skuCategory.setCreateTime(entity.getCreateTime());
                    skuCategory.setUpdateTime(entity.getUpdateTime());
                    skuCategory.setIsDeleted(entity.getIsDeleted());
                    List<SkuCategory> children = buildTree(entity.getId(), list);
                    skuCategory.setChildren(children);
                    return skuCategory;
                }).toList();
    }
}
